package com.example.huanhang.changzhouapp.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.huanhang.changzhouapp.R;
import com.gyf.barlibrary.ImmersionBar;

import java.text.DecimalFormat;

import es.voghdev.pdfviewpager.library.PDFViewPager;
import es.voghdev.pdfviewpager.library.RemotePDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
import es.voghdev.pdfviewpager.library.remote.DownloadFile;
import es.voghdev.pdfviewpager.library.util.FileUtil;

public class ShowPdfActivity extends AppCompatActivity implements DownloadFile.Listener {

    private PDFViewPager mPdfViewPager;
    private PDFPagerAdapter adapter;
    private RemotePDFViewPager mRemotePDFViewPager;
    private ProgressBar mMainProgress;
    private AppCompatImageView mIvBack;
    private AppCompatTextView mTvTitle;
    private TextView mTvJindu;
    private LinearLayout mLlPro;
    DecimalFormat df = new DecimalFormat("0.0");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_pdf);
        initView();
        initData();
    }

    private void initView() {
        mPdfViewPager = (PDFViewPager) findViewById(R.id.pdfViewPager);
        mMainProgress = (ProgressBar) findViewById(R.id.main_progress);
        mIvBack = (AppCompatImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvTitle = (AppCompatTextView) findViewById(R.id.tv_title);
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
        mTvJindu = (TextView) findViewById(R.id.tv_jindu);
        mLlPro = (LinearLayout) findViewById(R.id.ll_pro);
    }

    private void initData() {
        mTvTitle.setText("查看文件");
        String pdfPath = getIntent().getStringExtra("pdfPath");
        String url = "http://211.159.176.93:8666/test/" + pdfPath;
        mRemotePDFViewPager = new RemotePDFViewPager(ShowPdfActivity.this, url, this);

    }


    @Override
    public void onSuccess(String url, String destinationPath) {
        mLlPro.setVisibility(View.GONE);
        adapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
        mRemotePDFViewPager.setAdapter(adapter);
        setContentView(mRemotePDFViewPager);
    }

    @Override
    public void onFailure(Exception e) {
        mLlPro.setVisibility(View.GONE);
        new AlertDialog.Builder(ShowPdfActivity.this)
                .setMessage("加载pdf失败\nError:" + e)
                .setPositiveButton("确定", null).show();

    }
    @Override
    public void onProgressUpdate(int progress, int total) {
        float i =(float) progress / total * 100;
        String pro = df.format(i) + "%";
        mTvJindu.setText("加载进度:"+pro+" 文件大小:"+getDataSize(total));
    }
    /**
     * 返回byte的数据大小对应的文本
     *
     * @param size
     * @return
     */
    public static String getDataSize(long size) {
        if (size < 0) {
            size = 0;
        }
        DecimalFormat formater = new DecimalFormat("####.00");
        if (size < 1024) {
            return size + "bytes";
        } else if (size < 1024 * 1024) {
            float kbsize = size / 1024f;
            return formater.format(kbsize) + "KB";
        } else if (size < 1024 * 1024 * 1024) {
            float mbsize = size / 1024f / 1024f;
            return formater.format(mbsize) + "MB";
        } else if (size < 1024 * 1024 * 1024 * 1024) {
            float gbsize = size / 1024f / 1024f / 1024f;
            return formater.format(gbsize) + "GB";
        } else {
            return String.valueOf(size);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.close();
            adapter = null;
        }
    }
}
